<?php

namespace Common\Model;

/**
 * Description of RoomServiceModel
 * @author wenhui
 * @modify wenhui 2017-12-01 创建
 */
class RoomServiceModel extends BaseModel {

    protected $trueTableName = 'tb_room_service';

    /**
     * 设置房间为维修房
     * @modify wenhui 2017-12-01 创建
     * @param  integer   $hotelId   客栈id
     * @param  integer   $roomTypeId    房态id
     * @param  integer   $roomId    房间id
     * @param  integer   $status    需要设置为干净/维修房
     * @param  integer   $starDate    开始时间戳
     * @param  integer   $endDate    结束时间戳
     */
    public function addService($hotelId, $roomTypeId, $roomId, $status, $startDate, $endDate) {
        $data = array(
            'hotelentity_id' => $hotelId,
            'roomtype_id' => $roomTypeId,
            'roominfo_id' => $roomId,
            'isservice' => $status,
            'startdate' => $startDate,
            'enddate' => $endDate,
        );

        return $this->add($data);
    }

    /**
     * 设置房间为维修房
     * @modify wenhui 2017-12-01 创建
     * @param  integer   $hotelId   客栈id
     * @param  integer   $roomTypeId    房态id
     * @param  integer   $roomId    房间id
     * @param  integer   $status    需要设置为干净/维修房
     * @param  integer   $startDate    开始时间戳
     * @param  integer   $endDate    结束时间戳
     */
    public function saveService($hotelId, $roomTypeId, $roomId, $status, $startDate, $endDate, $delete) {
        $option = array(
            'hotelentity_id' => $hotelId,
            'roomtype_id' => $roomTypeId,
            'roominfo_id' => $roomId,
        );

        $data = array(
            'isservice' => $status,
            'startdate' => $startDate,
            'enddate' => $endDate,
            'isdelete' => $delete,
        );

        return $this->where($option)->save($data) === false ? false : true;
    }

    /**
     * 根据RoomTypeID获取维修房列表
     * @add wenhui 2017-12-01
     * @param integer   $roomTypeId 房型ID
     * @param integer   $roomId 房间ID
     * @param integer   $hotelId 客栈ID
     * @param integer   $delete
     * @param integer   $egtEndDate 结束时间是否大于今天
     * @return array      维修房数组
     */
    public function getServiceList($roomTypeId, $roomId = null, $hotelId = null, $delete = null, $egtToday = false) {
        $option = array();
        
        if (! is_array($roomTypeId)) {
            $roomTypeId = array( $roomTypeId );
        }
        $option['roomtype_id'] = array( 'in',  $roomTypeId );

        if (!is_null($delete)) {
            $option['isdelete'] = $delete;
        }

        if (!is_null($roomId)) {
            $option['roominfo_id'] = $roomId;
        }

        if ($egtToday) {
            $option['enddate'] = array('egt', strtotime(date('Y-m-d')));
        }

        $field = 'id,hotelentity_id,roomtype_id,roominfo_id,isservice,startdate,enddate';
        $data = $this->where($option)->field($field)->select();

        return empty($data) ? array() : $data;
    }

    /**
     * 根据RoomTypeID获取维修房列表
     * @add wenhui 2017-12-01
     * @param integer   $roomTypeId 房型ID
     * @param integer   $egtEndDate 结束时间是否大于今天
     * @return array      维修房数组
     */
    public function getServiceListByRoomId($roomId, $egtToday = false) {
        $option = array();

        if (! is_array($roomId)) {
            $roomId = array( $roomId );
        }
        $option['roominfo_id'] = array( 'in',  $roomId );
        $option['isdelete'] = NOT_DELETE;

        if ($egtToday) {
            $option['enddate'] = array('egt', strtotime(date('Y-m-d')));
        }

        $field = 'id,hotelentity_id,roomtype_id,roominfo_id,isservice,startdate,enddate';
        $data = $this->where($option)->field($field)->select();

        return empty($data) ? array() : $data;
    }
}

